package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.QuestionBankDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.QuestionBank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = QuestionBankService.class)
@Transactional
public class QuestionBankServiceImpl implements QuestionBankService {
    @Autowired
    private QuestionBankDao questionBankDao;

    @Override
    public void add(QuestionBank questionBank) {
        questionBankDao.add(questionBank);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //获取分页参数
        String queryString = queryPageBean.getQueryString();
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        PageHelper.startPage(currentPage, pageSize);
        Page<QuestionBank> page = questionBankDao.findByCondition(queryString);
        long total = page.getTotal();
        List<QuestionBank> rows = page.getResult();
        PageResult pageResult = new PageResult(total, rows);
        return pageResult;
    }

    @Override
    public void edit(QuestionBank questionBank) {
        questionBankDao.edit(questionBank);
    }

    @Override
    public void delete(Integer id) {
        questionBankDao.delete(id);
    }

    @Override
    public QuestionBank findById(Integer id) {
        return questionBankDao.findById(id);
    }
}
